//
//  YXHomeViewController.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/20.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXHomeViewController.h"
#import "YXDrawerViewController.h"
#import "YXMessageViewController.h"
#import "YXHomeFenRunView.h"
#import "YXHomeMenuView.h"
#import "YXHomeCarMenuView.h"
#import "SDCycleScrollView.h"
#import "YXHomeManageTypeViewController.h"
#import "YXHomeMeunTypeViewController.h"
#import "YXForumViewController.h"
#import "YXMachineMaterialViewController.h"
#import "YXWebViewController.h"
#import "YXClassRoomViewController.h"
#import "YXLoginAndRegistViewController.h"
#import "YXHomeViewModel.h"
@interface YXHomeViewController ()<SDCycleScrollViewDelegate>
@property (nonatomic ,strong) UIScrollView *scrollView;
@property (nonatomic ,strong) SDCycleScrollView * cycleScrollView;
@property (nonatomic ,strong) YXHomeFenRunView *fenrunView;
@property (nonatomic ,strong) YXHomeMenuView *menuView;
@property (nonatomic ,strong) YXHomeViewModel *homeViewModel;

@end

@implementation YXHomeViewController

- (IBAction)mineItemAction:(UIBarButtonItem *)sender {
    [[YXDrawerViewController shareDrawer] openLeftMenu];
    
//    YXLoginAndRegistViewController *loginVC = [[YXLoginAndRegistViewController alloc] init];
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
//    [self.navigationController presentViewController:nav animated:YES completion:nil];
    
}
- (IBAction)messageItemAction:(UIBarButtonItem *)sender {
    [self.navigationController pushViewController:[[YXMessageViewController alloc] init] animated:YES];
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _scrollView.backgroundColor = color_backColor;
        if (iPhone6Plus_6sPlus) {
            _scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight * 2 - 340);
        }else {
            _scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight * 2 - 200);
        }
    }
    return _scrollView;
}

- (YXHomeViewModel *)homeViewModel {
    if (!_homeViewModel) {
        _homeViewModel = [[YXHomeViewModel alloc] init];
    }
    return _homeViewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"卡友";
    [self.view addSubview:self.scrollView];
    [self createThirdBtnViewUI];
    [self createBalanceViewUI];
    [self createFenRunViewUI];
    [self createMenuViewUI];
    [self createCarMenuViewUI];
}
- (void)createThirdBtnViewUI {
    
    UIView *backView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 140))];
    backView.backgroundColor = color_globalColor;
    [self.scrollView addSubview:backView];
    
    NSArray *imageArr = @[@"014",@"013",@"012"];
    NSArray *titleArr = @[@"二维码",@"添加合伙人",@"钱包"];
    for (int i = 0 ; i < titleArr.count; i ++ ) {
        UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        btn.frame = CGRectMake(self.view.frame.size.width/3 * i + self.view.frame.size.width/3/2 -50 , 20, 100, 100);
        btn.tag = 500 + i;
        [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",imageArr[i]]] forState:(UIControlStateNormal)];
        [btn setTitle:titleArr[i] forState:(UIControlStateNormal)];
        [btn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [btn setImgViewStyle:(ButtonImgViewStyleTop) imageSize:(CGSizeMake(60, 60)) space:10];
        [btn addTarget:self action:@selector(thirdBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [backView addSubview:btn];
    }
}

- (void)thirdBtnAction:(UIButton *)sender {
    NSInteger index = sender.tag - 500;
    YXHomeManageTypeViewController *manageVC = [[YXHomeManageTypeViewController alloc] init];
    manageVC.type = index;
    manageVC.title = sender.titleLabel.text;
    [self.navigationController pushViewController:manageVC animated:YES];
}


- (void)createBalanceViewUI {
    _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 140, kScreenWidth, 200) delegate:self placeholderImage:nil];
    [self.scrollView addSubview:self.cycleScrollView];
    [YXHomeViewModel getQuerySelectBannerListCategory:@"2" Type:nil ModelList:^(NSArray *listArr,YXBannerModel *model) {
        _cycleScrollView.imageURLStringsGroup = listArr;
        
    } Failure:^(NSError *error) {
        [self showAlertWithTitle:@"提示" Messge:@"维护中，请稍等！"];
    }];
}

- (void)createFenRunViewUI {
    self.fenrunView = [[YXHomeFenRunView alloc] initWithFrame:(CGRectMake(0, _cycleScrollView.H + _cycleScrollView.Y + 10, kScreenWidth, 80)) withDataArray:nil];
    [self.scrollView addSubview:self.fenrunView];

    [YXHomeViewModel getQueryGetGrossProfitPid:@"10000" ModelList:^(NSString *body) {
        
    } Failure:^(NSError *error) {
        
    } ];
}

- (void)createMenuViewUI {
    
    self.menuView = [[YXHomeMenuView alloc] initWithFrame:(CGRectMake(0, _fenrunView.H + _fenrunView.Y + 10, kScreenWidth, 150)) withDataArray:nil];
    [self.scrollView addSubview: self.menuView];
    
    YXWeakSelf
    [self.menuView setSelectBtnClickBlock:^(NSInteger index) {
        YXHomeMeunTypeViewController *typeVC = [[YXHomeMeunTypeViewController alloc] init];
        typeVC.type = index;
        [weakSelf.navigationController pushViewController:typeVC animated:YES];
    }];
}


- (void)createCarMenuViewUI {
    
    YXHomeCarMenuView *carLifeMenuView = [[YXHomeCarMenuView alloc] initWithFrame:(CGRectMake(0, _menuView.H + _menuView.Y + 10, kScreenWidth, 140)) withTitle:@"卡友好生活" withBtnTitleArr:@[@"卡友论坛",@"机具物料"] withBtnImageArr:@[@"007",@"006"]];
    [self.scrollView addSubview:carLifeMenuView];
    [carLifeMenuView setSelectClickBtnBlock:^(NSInteger index,NSString *title) {
        if (index == 0) {
            YXForumViewController *forumVC = [[YXForumViewController alloc] init];
            [self.navigationController pushViewController:forumVC animated:YES];
        }else {
            YXMachineMaterialViewController *mmVC = [[YXMachineMaterialViewController alloc] init];
            mmVC.typeView = Machine_MaterialView;
            [self.navigationController pushViewController:mmVC animated:YES];
        }
    }];
    
    YXHomeCarMenuView *carYoungMenuView = [[YXHomeCarMenuView alloc] initWithFrame:(CGRectMake(0, carLifeMenuView.H + carLifeMenuView.Y + 10, kScreenWidth, 140)) withTitle:@"年轻无极限" withBtnTitleArr:@[@"奖励公告",@"创业扶持",@"助飞梦想"] withBtnImageArr:@[@"005",@"004",@"003"]];
    [self.scrollView addSubview:carYoungMenuView];
    
    [carYoungMenuView setSelectClickBtnBlock:^(NSInteger index, NSString *title) {
        YXWebViewController *webVC = [[YXWebViewController alloc] init];
        webVC.title = title;
        webVC.type = index + 1;
        [self.navigationController pushViewController:webVC animated:YES];
    }];
    
    YXHomeCarMenuView *carClassMenuView = [[YXHomeCarMenuView alloc] initWithFrame:(CGRectMake(0, carYoungMenuView.H + carYoungMenuView.Y + 10, kScreenWidth, 140)) withTitle:@"卡友辅助班" withBtnTitleArr:@[@"卡友课堂",@"推荐办卡"] withBtnImageArr:@[@"002",@"001"]];
    [self.scrollView addSubview:carClassMenuView];
    
    [carClassMenuView setSelectClickBtnBlock:^(NSInteger index,NSString *title) {
        YXClassRoomViewController *crVC = [[YXClassRoomViewController alloc] init];
        crVC.title = title;
        [self.navigationController pushViewController:crVC animated:YES];
    }];
    
}




@end
